package com.ridaizu.dao.impl;

import static com.google.common.base.Preconditions.checkNotNull;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import com.ridaizu.dao.DaoAdapter;
import com.ridaizu.dao.db.DBAccess;
import com.ridaizu.model.Model;
import com.ridaizu.model.Transit;
import com.ridaizu.model.search.SearchModel;
import com.ridaizu.util.DateUtil;

@ManagedBean(name = "transitDao")
@ApplicationScoped
public class TransitDao extends DaoAdapter {

	private static final long serialVersionUID = 1L;

	public int selectCount(SearchModel searchModel) throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("select count(id) from d_transit \n");
		sql.append(searchModel.getSelectCountSql());

		Object[] params = searchModel.getConditionList().toArray();

		ResultSet rs = DBAccess.query(sql.toString(), params);

		if (rs == null) {
			return -1;
		}

		rs.next();

		return rs.getInt(1);
	}

	public List<Transit> selectTransitList() throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("SELECT id, transit_date FROM d_transit \n");

		ResultSet rs = DBAccess.query(sql.toString());

		List<Transit> modelList = new ArrayList<Transit> ();
		while (rs.next()) {
			Transit transit = new Transit();
			transit.setId(rs.getInt("id"));
			transit.setName(DateUtil.dateToString(rs.getTimestamp("transit_date"), DateUtil.LONG_DATE));

			modelList.add(transit);
		}

		rs.close();

		return modelList;
	}

	public List<Transit> selectTransitList(SearchModel searchModel) throws SQLException {

		StringBuffer sql = new StringBuffer();
		sql.append("SELECT * FROM d_transit \n");
		sql.append(searchModel.getSelectSql());

		Object[] params = searchModel.getConditionList().toArray();

		ResultSet rs = DBAccess.query(sql.toString(), params);

		List<Transit> modelList = new ArrayList<Transit> ();
		while (rs.next()) {
			modelList.add((Transit)getModel(rs));
		}

		rs.close();

		return modelList;
	}

	@Override
	public Model getModel(ResultSet rs) throws SQLException {

		checkNotNull(rs);
		Transit transit = new Transit();
		transit.setId(rs.getInt("id"));
		transit.setTransitDate(rs.getTimestamp("transit_date"));
		transit.setGoodsTotalCount(rs.getInt("goods_total_count"));
		transit.setGoodsTotalPrice(rs.getDouble("goods_total_price"));
		transit.setFreightTotal(rs.getDouble("freight_total"));
		transit.setArriveFlg(rs.getBoolean("arrive_flg"));
		transit.setArriveDate(rs.getTimestamp("arrive_date"));
		transit.setRgtOpt(rs.getString("rgt_opt"));
		transit.setRgtDate(rs.getTimestamp("rgt_date"));
		transit.setUpdOpt(rs.getString("upd_opt"));
		transit.setUpdDate(rs.getTimestamp("upd_date"));

		return transit;
	}

	@Override
	public int insert(Model model) {

		checkNotNull(model);

		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO d_transit \n");
		sql.append("( \n");
		sql.append("transit_date, goods_total_count, goods_total_price, freight_total, \n");
		sql.append("arrive_flg, arrive_date, rgt_opt, rgt_date, \n");
		sql.append("upd_opt, upd_date \n");
		sql.append(") \n");
		sql.append("VALUES \n");
		sql.append("(?, ?, ?, ?, ?, ?, ?, ?, ?, ?); \n");

		Transit transit = (Transit) model;

		Object[] params = new Object[] {
				transit.getTransitDate(), transit.getGoodsTotalCount(), transit.getGoodsTotalPrice(),
				transit.getFreightTotal(), transit.isArriveFlg(), transit.getArriveDate(),
				transit.getRgtOpt(), transit.getRgtDate(), transit.getUpdOpt(),
				transit.getUpdDate()
		};

		return DBAccess.update(sql.toString(), params);
	}

	@Override
	public int update(Model model) {

		checkNotNull(model);

		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE d_transit \n");
		sql.append("SET \n");
		sql.append("transit_date = ?, \n");
		sql.append("goods_total_count = ?, \n");
		sql.append("goods_total_price = ?, \n");
		sql.append("freight_total = ?, \n");
		sql.append("arrive_flg = ?, \n");
		sql.append("arrive_date = ?, \n");
		sql.append("rgt_opt = ?, \n");
		sql.append("rgt_date = ?, \n");
		sql.append("upd_opt = ?, \n");
		sql.append("upd_date = ? \n");
		sql.append("WHERE id = ? \n");

		Transit transit = (Transit) model;

		Object[] params = new Object[] {
				transit.getTransitDate(), transit.getGoodsTotalCount(), transit.getGoodsTotalPrice(),
				transit.getFreightTotal(), transit.isArriveFlg(), transit.getArriveDate(),
				transit.getRgtOpt(), transit.getRgtDate(), transit.getUpdOpt(),
				transit.getUpdDate(), transit.getId()
		};

		return DBAccess.update(sql.toString(), params);
	}

	public int delete(int id) {
		String sql = "DELETE FROM d_transit WHERE id=?;";

		return DBAccess.update(sql.toString(), id);
	}

}
